package com.model;

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import android.util.Log;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfAnnotation;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfReport {
	
	private List<PdfAnno> annotations;
	private PdfReader reader;
	
	
	public PdfReport(){	
	}
	
	public PdfReport(String file){
		try {
			reader = new PdfReader(file);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	
	public void save(String file){
		
		try {
			
			FileOutputStream fileOutputStream= new FileOutputStream(file);
	    	PdfStamper stamper=new PdfStamper(reader, fileOutputStream);
	    	PdfWriter writer=stamper.getWriter();
	    	
	    	for (PdfAnno anno : annotations) {
	    		Rectangle size=reader.getPageSize(anno.getPage());
	    		
	    		float llx=anno.getX()*size.getWidth();
	    		float lly=anno.getY()*size.getHeight();
	    		float urx=llx+20;
	    		float ury=lly+20;
	    		
	    		stamper.addAnnotation(
						PdfAnnotation.createText(writer,
						new Rectangle(llx, lly, urx, ury),
						"Comment", anno.getContent(), false, anno.getType()),anno.getPage());			
			}
	    	
	    	stamper.close();
	    	fileOutputStream.close();
	
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 					
	}	
	
	public void addAnnotation(List<PdfAnno> list){
		annotations=list;
	}

}
